/*
 * Created on Nov 18, 2003
 */
package org.vectrics.company;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
import org.vectrics.common.cache.Cachable;
import org.vectrics.common.db.Auditable;
import org.vectrics.common.db.OwnedObject;
import org.vectrics.common.db.RecordObject;
import org.vectrics.domain.Domain;
import org.vectrics.location.Address;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;

public class Company implements RecordObject, Auditable, Serializable, Cachable, OwnedObject {

    public static final long serialVersionUID = 2242362342355L;

    public static String OBJECT_TYPE = "company";

    public static String PRIVILEGE_COMPANY_VIEW = "company.view";

    private transient static Logger log = Logger.getLogger(Company.class);

    private String name = null;

    private Long id = null;

    private String referenceId = null;

    private String phone = null;

    private String phone2 = null;

    private Company parent = null;

    private Address address = null;

    private Location location = null;

    private boolean voided = false;

    private Date dateLastChange = new Date();

    private Person createdBy = null;

    private Date createdDate = null;

    private Person lastModifiedBy = null;

    private Date lastModifiedDate = null;

    private Domain domain = null;

    private Sector sector = null;

    public Company() {
        super();

    }

    /**
     * @param parent
     *            The parent to set.
     */
    public void setParent(Company parent) {
        this.parent = parent;
    }

    public Company getParent() {
        return (parent);
    }

    /**
     * @return
     */
    public boolean getVoided() {
        return voided;
    }

    /**
     * @param active
     */
    public void setVoided(boolean active) {
        this.voided = active;
    }

    /**
     * @return Returns the location.
     */
    public Location getLocation() {
        return location;
    }

    /**
     * @param location
     *            The location to set.
     */
    public void setLocation(Location location) {
        this.location = location;
    }

    /**
     * @return Returns the referenceId.
     */
    public String getReferenceId() {
        return referenceId;
    }

    /**
     * @param referenceId
     *            The referenceId to set.
     */
    public void setReferenceId(String referenceId) {
        this.referenceId = referenceId;
    }

    /**
     * @return
     */
    public Date getDateLastChange() {
        return dateLastChange;
    }

    /**
     * @param dateLastChange
     */
    public void setDateLastChange(Date dateLastChange) {
        this.dateLastChange = dateLastChange;
    }

    /**
     * @return The id (primary key) of the company.
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return The company name.
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return The company's primary phone number.
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return The company's secondary phone.
     */
    public String getPhone2() {
        return phone2;
    }

    /**
     * @param phone2
     */
    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    /**
     * @return Returns the main address of the company. Addresses of divisions
     *         can be found under the Company object representing that division.
     */
    public Address getAddress() {
        return address;
    }

    /**
     * @param address
     *            The address to set.
     */
    public void setAddress(Address address) {
        this.address = address;
    }

    public String toString() {
        if (this.getId() == null)
            return ("null");
        return (this.getId().toString());
    }

    /**
     * @return Returns the createdBy.
     */
    public Person getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy
     *            The createdBy to set.
     */
    public void setCreatedBy(Person createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return Returns the createdDate.
     */
    public Date getCreatedDate() {
        return createdDate;
    }

    /**
     * @param createdDate
     *            The createdDate to set.
     */
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    /**
     * @return Returns the lastModifiedBy.
     */
    public Person getLastModifiedBy() {
        return lastModifiedBy;
    }

    /**
     * @param lastModifiedBy
     *            The lastModifiedBy to set.
     */
    public void setLastModifiedBy(Person lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    /**
     * @return Returns the lastModifiedDate.
     */
    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    /**
     * @param lastModifiedDate
     *            The lastModifiedDate to set.
     */
    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public boolean logEntityChanges() {
        return (true);
    }

    public boolean isPersonInGroup(Person person, CompanyPersonGroupType companyPersonGroupType) {
        boolean result = false;

        CompanyPerson companyPerson = CoreServiceLocator.getCompanyService().findCompanyPerson(this, person);
        if (companyPerson != null) {
            CompanyPersonRole companyPersonRole = companyPerson.getCurrentRole();
            if (companyPersonRole != null) {
                result = companyPersonRole.hasGroupMembership(companyPersonGroupType);
            }
        }
        return (result);
    }

    /**
     * @return Returns the domain.
     */
    public Domain getDomain() {
        return domain;
    }

    /**
     * @param domain
     *            The domain to set.
     */
    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(getName()).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!obj.getClass().isAssignableFrom(getClass())) {
            return false;
        }
        Company other = (Company) obj;

        return new EqualsBuilder().append(this.getName(), other.getName()).isEquals();
    }

}
